package com.yangjie.autotest;

import java.io.File;

import javax.inject.Inject;

import com.yangjie.autotest.testcase.TestCaseExecutor;
import com.yangjie.autotest.testcase.TestCasesReader;
import com.yangjie.autotest.testcase.xml.TestCases;
import com.yangjie.log.LoggerIF;

public class Framework {
	@Inject
	private LoggerIF logger = null;
	
	@Inject
	TestCasesReader testCaseReader = null;
	
	@Inject
	TestCaseExecutor testCaseExec = null;
	
	public TestCaseExecutor getTestCaseExec() {
		return testCaseExec;
	}

	public void setTestCaseExec(TestCaseExecutor testCaseExec) {
		this.testCaseExec = testCaseExec;
	}

	public TestCasesReader getTestCaseReader() {
		return testCaseReader;
	}

	public void setTestCaseReader(TestCasesReader testCaseReader) {
		this.testCaseReader = testCaseReader;
	}

	private String testCasesPath = null;
	
	
	public String getTestCasesPath() {
		return testCasesPath;
	}

	public void setTestCasesPath(String testCasesPath) {
		this.testCasesPath = testCasesPath;
	}

	private String configFile;
	public LoggerIF getLogger() {
		return logger;
	}

	public void setLogger(LoggerIF logger) {
		this.logger = logger;
	}

	public String getConfigFile() {
		return configFile;
	}

	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}
	
	public void runTestCasesInDir(String filePath, String testCaseName) {
		File file = new File(filePath);
		if(!file.exists()) {
			logger.error("Can't find the directory of file of testcases:" + filePath);
		}
		if(file.isDirectory()) {
			String[] files = file.list();
			for(int i = 0; i < files.length; i++) {
				runTestCasesInDir(filePath + File.separator + files[i], testCaseName);
			}
		} else {
			if(filePath.toLowerCase().endsWith("xml")) {
				TestCases testCases = testCaseReader.readTestCases(filePath);
				if(testCases == null) {
					logger.error("Read test cases failed, file:" + filePath);
				}
				
				testCaseExec.runTestCases(testCases, testCaseName);
			}
		}
	}

	public void run(String testCasesPathFromCMD, String testCaseName) {
//		logger.debug();
		if(testCasesPathFromCMD != null) {
			testCasesPath = testCasesPathFromCMD;
		}
		logger.debug("testCasesPath:" + testCasesPath);
		runTestCasesInDir(testCasesPath, testCaseName);
		
		
	
	}
	
	
}
